package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.GoodsUserCouponDTO;
import top.zywork.query.GoodsUserCouponQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GoodsUserCouponService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.GoodsUserCouponVO;

import java.util.List;

/**
 * GoodsUserCouponController控制器类<br/>
 *
 * 创建于2019-08-02<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/goods-user-coupon")
@Slf4j
public class GoodsUserCouponController extends BaseController {

    private GoodsUserCouponService goodsUserCouponService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存GoodsUserCouponVO")
    public ResponseStatusVO save(@RequestBody @Validated GoodsUserCouponVO goodsUserCouponVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsUserCouponService.save(BeanUtils.copy(goodsUserCouponVO, GoodsUserCouponDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存GoodsUserCouponVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<GoodsUserCouponVO> goodsUserCouponVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsUserCouponService.saveBatch(BeanUtils.copyListObj(goodsUserCouponVOList, GoodsUserCouponDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除GoodsUserCouponVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        goodsUserCouponService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除GoodsUserCouponVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        goodsUserCouponService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新GoodsUserCouponVO")
    public ResponseStatusVO update(@RequestBody @Validated GoodsUserCouponVO goodsUserCouponVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = goodsUserCouponService.update(BeanUtils.copy(goodsUserCouponVO, GoodsUserCouponDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新GoodsUserCouponVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<GoodsUserCouponVO> goodsUserCouponVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsUserCouponService.updateBatch(BeanUtils.copyListObj(goodsUserCouponVOList, GoodsUserCouponDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结GoodsUserCouponVO")
    public ResponseStatusVO active(@RequestBody GoodsUserCouponVO goodsUserCouponVO) {
        goodsUserCouponService.update(BeanUtils.copy(goodsUserCouponVO, GoodsUserCouponDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结GoodsUserCouponVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<GoodsUserCouponVO> goodsUserCouponVOList) {
        goodsUserCouponService.updateBatch(BeanUtils.copyListObj(goodsUserCouponVOList, GoodsUserCouponDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        GoodsUserCouponVO goodsUserCouponVO = new GoodsUserCouponVO();
        Object obj = goodsUserCouponService.getById(id);
        if (obj != null) {
            goodsUserCouponVO = BeanUtils.copy(obj, GoodsUserCouponVO.class);
        }
        return ResponseStatusVO.ok("查询成功", goodsUserCouponVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = goodsUserCouponService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsUserCouponVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody GoodsUserCouponQuery goodsUserCouponQuery) {
        PagerDTO pagerDTO = goodsUserCouponService.listAllByCondition(goodsUserCouponQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsUserCouponVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody GoodsUserCouponQuery goodsUserCouponQuery) {
        PagerDTO pagerDTO = goodsUserCouponService.listPageByCondition(goodsUserCouponQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsUserCouponVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 用户根据条件查询所有的优惠券信息
     * @param goodsUserCouponQuery
     * @return
     */
    @PostMapping("user/all-cond")
    public ResponseStatusVO userListAllByCondition(@RequestBody GoodsUserCouponQuery goodsUserCouponQuery) {
        goodsUserCouponQuery.setUserId(SecurityUtils.getJwtUser().getUserId());
        return listAllByCondition(goodsUserCouponQuery);
    }

    /**
     * 用户删除优惠券-禁用掉用户优惠券
     * @param id 用户优惠券id
     * @return
     */
    @GetMapping("user/remove/{id}")
    @SysLog(description = "用户删除商品优惠券")
    public ResponseStatusVO userRemoveById(@PathVariable("id") Long id) {
        GoodsUserCouponVO goodsUserCouponVO = new GoodsUserCouponVO();
        goodsUserCouponVO.setId(id);
        goodsUserCouponVO.setIsActive((byte)1);
        goodsUserCouponService.update(goodsUserCouponVO);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @Autowired
    public void setGoodsUserCouponService(GoodsUserCouponService goodsUserCouponService) {
        this.goodsUserCouponService = goodsUserCouponService;
    }
}
